package com.muqi.app.qlearn.adapter;

import java.util.List;

import com.muqi.app.qlearn.modles.AddressBean;
import com.muqi.app.qlearn.student.R;

import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AddressAdapter extends BaseAdapter {
    private List<AddressBean> mDataList;
    private Context mContext;
    private String addressType;
    private String mCheckId;
    private onCheckListener mCheckListener;

    public interface onCheckListener{
    	void onCheck(int position);
    }
    public void setCheckListener(onCheckListener mCheckListener){
        this.mCheckListener = mCheckListener;
    }

    public String getCheckId() {
        return mCheckId;
    }
    
    public void setCheckId(String mCheckId) {
        this.mCheckId = mCheckId;
    }

    public AddressAdapter(Context context, List<AddressBean> data,String addressType){
        mContext = context;
        mDataList = data;
        this.addressType = addressType;
    }
    
    @Override
    public int getCount() {
        return mDataList==null?0:mDataList.size();
    }

    @Override
    public Object getItem(int position) {
        if(mDataList==null || position>=mDataList.size()) return null;
        return mDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if(null==convertView){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_address,null);
            holder.mNameTxt = (TextView)convertView.findViewById(R.id.address_item_name_txt);
            holder.mNameTxt.getPaint().setFakeBoldText(true);
            holder.mTelTxt = (TextView)convertView.findViewById(R.id.address_item_tel_txt);
            holder.mAddrTxt = (TextView)convertView.findViewById(R.id.address_item_address_txt);
            holder.mEditBtn = (ImageView)convertView.findViewById(R.id.address_item_edit_btn);
            holder.checkBox = (ImageView)convertView.findViewById(R.id.checkbox);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }
        
        AddressBean data = mDataList.get(position);
        holder.mTelTxt.setText(data.getLianxidianhua());
        holder.mNameTxt.setText(data.getLianxiren());
        String addrDetail = data.getProvince_name() + " " + data.getCity_name() +
                " " + data.getDistrict_name() + " " + data.getDizhi();
        holder.mEditBtn.setTag(data);
        if(TextUtils.equals(data.getChangyong(),"1")){
            holder.mAddrTxt.setText(Html.fromHtml("<html><body><font color='#339cfc'><strong><tt>"+"默认地址 "+"<tt></strong></font>"+addrDetail+"</body><html>"));
        }else{
            holder.mAddrTxt.setText( addrDetail);
        }
        
        holder.checkBox.setTag(data);
        if("manage_address".equals(addressType)){
        	holder.checkBox.setVisibility(View.GONE);
        }else{
        	holder.checkBox.setVisibility(View.VISIBLE);
        	if(TextUtils.equals(data.getDizhi_id(), mCheckId)){
        		holder.checkBox.setImageResource(R.drawable.check_select_addr_yes);
        	}else{
        		holder.checkBox.setImageResource(R.drawable.check_select_addr_no);
        	}
        	holder.checkBox.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					mCheckListener.onCheck(position);
				}
			});
        }
//        holder.mEditBtn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//            	AddressBean d = (AddressBean) v.getTag();
//                Intent intent = new Intent(mContext, AddModifyAddressActivity.class);
//                intent.putExtra("data", d);
//                ((Activity) mContext).startActivityForResult(intent,100);
//            }
//        });
        return convertView;
    }
    
    class ViewHolder{
    	ImageView checkBox;
        TextView mNameTxt;
        TextView mTelTxt;
        TextView mAddrTxt;
        ImageView mEditBtn;
    }
   
}
